// router.js

import { createRouter, createWebHistory } from 'vue-router';
import login from "./vueview/login.vue";
import index from "./vueview/index.vue"
import board from "./vueview/board.vue"
import Orderfood from  "./vueview/Order-food.vue"
import userdetails from  "./vueview/User-details.vue"
import hotfood from "./vueview/hotfood.vue"
import colddish from "./vueview/coldDish.vue"
import meat from "./vueview/Meat.vue"
import drink from "./vueview/Drinks.vue"
import seafood from "./vueview/seafood.vue"
import soup from "./vueview/Soup.vue"
import staple from "./vueview/Staple.vue"
import searchfood from "./vueview/Searchfood.vue"
import specialmenu from "./vueview/Specialmenu.vue"
import itemdetail from "./vueview/itemDetail.vue"
import packitem from "./vueview/packitem.vue"
import orderList from "./vueview/Order_List.vue"
import orderedList from "./vueview/ordered-list.vue"
import Orderedpage from "./vueview/Orderedpage.vue"
import orderedselect from "./vueview/orderedpageselect.vue"
import recommenditem from "./vueview/recommenditem.vue"
import itempackdetail from  "./vueview/itempackdetail.vue"
import foodmannger from  "./vueview/FoodMannger.vue"
import selectOrder from "./vueview/select-order.vue"
import mangerindex from "./vueview/manger-index.vue"
import usermanager from "./vueview/usermanager.vue"
import RechargeCard from "@/vueview/RechargeCard.vue";
import OpenComputer from "@/components/computerComponents/OpenComputer.vue";
import UseComputerHistory from "@/components/computerComponents/UseComputerHistory.vue";
import StartUseComputer from "@/components/computerComponents/StartUseComputer.vue";
import CloseComputer from "@/components/computerComponents/CloseComputer.vue";
import CustomerService from "@/vueview/CustomerService.vue";
import manageitem from "./vueview/manageItem.vue"
import ordermanager from "./vueview/orderManage.vue";
import couponmanager from "./vueview/couponmanage.vue"
import commentmanage from "./vueview/commendmanage.vue"
import manaageitempack from "./vueview/manageitempack.vue"
import earningsmanage from "./vueview/Earningsmanage.vue"
import ManagerCustomer from "@/vueview/CustomerManager.vue";
import Points from "@/vueview/Points.vue";
import ComputerManager from "@/vueview/ComputerManager.vue";
import PointsManager from "@/vueview/PointsManager.vue";
import PointsEventManager from "@/vueview/Points-PointsEventManager.vue";
import PointsLotteryManager from "@/vueview/Points-LotteryManager.vue"
import PointsExchangeManager from "@/vueview/Points-ExchangeManager.vue";
import RechargeManager from "@/vueview/RechargeManager.vue";
import RoleManagerPage from "@/vueview/RoleManagerPage.vue";
import ImageManager from "@/vueview/ImageManager.vue";
import PersonalInfo from "@/vueview/PersonalInfo.vue";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login',name: 'Login', component: login },
        { path: '/index',name: 'Index', component: index },
        { path: '/board', component: board , redirect: '/board/openComputer',
            children: [
                {path: '/board/openComputer', component: OpenComputer},
                {path: '/board/useHistory', component: UseComputerHistory},
                {path: '/board/startUseComputer', name:'startUseComputer', component: StartUseComputer},
                {path: '/board/closeComputer', name:'closeComputer', component: CloseComputer}
            ]
        },

        { path: '/orderfood',redirect:'/orderfood/recommenditem', component: Orderfood ,
            children: [
                { path: '/orderfood/hotfood', component: hotfood },
                { path: '/orderfood/orderList', component: orderList},
                { path: '/orderfood/colddish', component: colddish},
                { path: '/orderfood/meat', component: meat },
                { path: '/orderfood/drink', component: drink},
                { path: '/orderfood/seafood', component: seafood},
                { path: '/orderfood/soup', component: soup},
                { path: '/orderfood/staple', component: staple},
                { path: '/orderfood/searchfood', component: searchfood},
                { path: '/orderfood/specialmenu', component: specialmenu},
                { path: '/orderfood/packitem', component: packitem},
                { path: '/orderfood/itemdetail', component: itemdetail},
                { path: '/orderfood/ordered', component: Orderedpage},
                { path: '/orderfood/orderedList', component: orderedList},
                { path: '/orderfood/orderedselect', component: orderedselect},
                { path: '/orderfood/recommenditem', component: recommenditem },
                { path: '/orderfood/itempackdetail', component: itempackdetail },
                { path: '/orderfood/selectOrder', component: selectOrder }

            ]},

        {path : '/manager',redirect:'/manager/mangerindex',name:'Manager', component: foodmannger,
            children: [
                {path: '/manager/mangerindex', component: mangerindex},
                {path: '/manager/usermanager', component: usermanager},
                {path: '/manager/manageitem', component: manageitem},
                {path: '/manager/ordermanager', component: ordermanager},
                {path: '/manager/couponmanager', component:couponmanager},
                {path: '/manager/commentmanage', component:commentmanage},
                {path: '/manager/manaageitempack', component:manaageitempack},
                {path: '/manager/earningsmanage', component:earningsmanage},
                {path: '/manager/computerManager', component: ComputerManager},
                { path: '/manager/ManagerCustomer', component: ManagerCustomer },
                {path: '/manager/roleManager', component: RoleManagerPage},
                {path: '/manager/imageManager', component: ImageManager},

                { path: '/manager/PointsManager', component: PointsManager,
                    children: [
                        { path: '/PointsEventManager', component: PointsEventManager },
                        { path: '/PointsLotteryManager', component: PointsLotteryManager },
                        { path: '/PointsExchangeManager', component: PointsExchangeManager },
                    ]
                },
                { path: '/manager/RechargeManager', component: RechargeManager,}
            ]
        },

        { path: '/RechargeCard', component: RechargeCard},
        { path: '/CustomerService', component: CustomerService},
        { path: '/RechargeCard', component: RechargeCard },
        { path: '/Points', component: Points },
        { path: '/CustomerService', component: CustomerService },
        { path: '/userdetails', component: userdetails },
        { path: '/personalInfo', component: PersonalInfo },
    ],
});

//路由导航守卫
/*import {useUserStore} from "@/stores/user.js";
//获取用户信息
const userStore = useUserStore()*/
router.beforeEach((to, from, next) => {
    const isLoginToken = localStorage.getItem('token')
    if (
        //检查用户是否已经登录
        !isLoginToken &&
        //!避免无限重定向
        to.name !== 'Login'
    ) {
        //将用户重定向到登录页面
        next ({ name: 'Login'})
    }
/*    else if (isLoginToken) {
        //如果token存在 说明已经登录
        try {
            const decodeToken = jwtDecode(isLoginToken)
            const username = decodeToken.claims.username
            console.log(username)
            if (to.name === 'index') {
                if (username === 'root1111') {
                    next({ name: 'Manager'})
                }else {
                    next({ name: 'Index'})
                }
            }else {
                next();
            }
        }catch (error) {
                console.error('Token decoding error token解析错误:', error)
                next({ name: 'Login'})
            }}*/
            /*
            /!*if (to.name === 'Login') {
                next();
            }else if (from.name === 'Login'){
                if (username === 'root1111') {
                    next({ name: 'Manager'})
                }else {
                    next({ name: 'Index'})
                }
            }*!/
        }catch (error) {
            console.error('Token decoding error token解析错误:', error)
            next({ name: 'Login'})
        }
    }*/
    else next()

})

export default router;
